import { Controller, Post, Get, Body, UnauthorizedException, HttpCode, HttpStatus, Param, Query, UseGuards, ParseIntPipe } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { UsersService } from './user.service';
import { Public } from "../login/decorators/public.decorator"

@Controller('/v1/api/users')
export class UserController {
  constructor(private userService: UsersService) {}

  // @Post('create')
  // async create(@Body() body: { username: string; password: string }) {
  //   const user = await this.userService.validateUser(body.username, body.password);
  //   if (!user) {
  //     throw new UnauthorizedException();
  //   }
  //   return this.userService.create(body.username, body.password);
  // }

  @UseGuards(AuthGuard('jwt'))
  @Public()
  @HttpCode(HttpStatus.OK)
  @Get('username')
  async getUserByname(@Query('username') username: string) {
    const user = await this.userService.findOneByName(username);
    if (!user) {
      throw new UnauthorizedException();
    }
    return user;
  }

  @UseGuards(AuthGuard('jwt'))
  @Public()
  @HttpCode(HttpStatus.OK)
  @Get('userid/:id')
  async getUserById(@Param('id') id: number | string) {
    const user = await this.userService.findOneById(id);
    if (!user) {
      throw new UnauthorizedException();
    }
    // return user;不能返回全部，特别是密码
    return {
      code: 200,
      message: '获取用户信息成功',
      data: {
        id: user._id,
        avatar: user.avatar,
        certified_driver: user.certified_driver,
        score: user.score,
        settledTime: user.settledTime,
        updateTime: user.updateTime,
        username: user.username,
      }
    };
  }
}